package com.yc.cloud.mapper;

import com.yc.cloud.entity.UmsResource;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 后台资源表 Mapper 接口
 * </p>
 *
 * @author JackGu
 * @since 2022-09-11
 */
@Mapper
public interface UmsResourceMapper extends BaseMapper<UmsResource> {
//    @Select("SELECT\n" +
//            "            ur.id id,\n" +
//            "            ur.create_time createTime,\n" +
//            "            ur.`name` `name`,\n" +
//            "            ur.url url,\n" +
//            "            ur.description description,\n" +
//            "            ur.category_id categoryId\n" +
//            "        FROM\n" +
//            "            ums_admin_role_relation ar\n" +
//            "        LEFT JOIN ums_role r ON ar.role_id = r.id\n" +
//            "        LEFT JOIN ums_role_resource_relation rrr ON r.id = rrr.role_id\n" +
//            "        LEFT JOIN ums_resource ur ON ur.id = rrr.resource_id\n" +
//            "        WHERE\n" +
//            "            ar.admin_id = #{adminId}\n" +
//            "        AND ur.id IS NOT NULL\n" +
//            "        GROUP BY\n" +
//            "            ur.id")

    @Select("SELECT\n" +
            "            ur.id id,\n" +
            "            MAX(ur.create_time) AS createTime,\n" +
            "            ur.`name` `name`,\n" +
            "            ur.url url,\n" +
            "            ur.description description,\n" +
            "            ur.category_id categoryId\n" +
            "        FROM\n" +
            "            ums_admin_role_relation ar\n" +
            "        LEFT JOIN ums_role r ON ar.role_id = r.id\n" +
            "        LEFT JOIN ums_role_resource_relation rrr ON r.id = rrr.role_id\n" +
            "        LEFT JOIN ums_resource ur ON ur.id = rrr.resource_id\n" +
            "        WHERE\n" +
            "            ar.admin_id = #{adminId}\n" +
            "        AND ur.id IS NOT NULL\n" +
            "        GROUP BY\n" +
            "            ur.id, ur.`name`, ur.url, ur.description, ur.category_id")
    List<UmsResource> getResourceList(@Param("adminId") Long adminId);
}
